package com.ruoyi.zsyh.controller;

import java.util.List;

import com.ruoyi.zsyh.domain.ZsyhAfterProduct;
import com.ruoyi.zsyh.service.IZsyhAfterProductService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zsyh.domain.ZsyhAfterProductType;
import com.ruoyi.zsyh.service.IZsyhAfterProductTypeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 售后产品分类Controller
 * 
 * @author yuexl
 * @date 2022-08-22
 */
@Controller
@RequestMapping("/zsyh/afterProductType")
public class ZsyhAfterProductTypeController extends BaseController
{
    private String prefix = "zsyh/afterProductType";

    @Autowired
    private IZsyhAfterProductTypeService zsyhAfterProductTypeService;
    @Autowired
    private IZsyhAfterProductService zsyhAfterProductService;

    @RequiresPermissions("zsyh:afterProductType:view")
    @GetMapping()
    public String afterProductType()
    {
        return prefix + "/afterProductType";
    }

    /**
     * 查询售后产品分类列表
     */
    @RequiresPermissions("zsyh:afterProductType:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZsyhAfterProductType zsyhAfterProductType)
    {
        startPage();
        List<ZsyhAfterProductType> list = zsyhAfterProductTypeService.selectZsyhAfterProductTypeList(zsyhAfterProductType);
        return getDataTable(list);
    }

    /**
     * 导出售后产品分类列表
     */
    @RequiresPermissions("zsyh:afterProductType:export")
    @Log(title = "售后产品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZsyhAfterProductType zsyhAfterProductType)
    {
        List<ZsyhAfterProductType> list = zsyhAfterProductTypeService.selectZsyhAfterProductTypeList(zsyhAfterProductType);
        ExcelUtil<ZsyhAfterProductType> util = new ExcelUtil<ZsyhAfterProductType>(ZsyhAfterProductType.class);
        return util.exportExcel(list, "售后产品分类数据");
    }

    /**
     * 新增售后产品分类
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存售后产品分类
     */
    @RequiresPermissions("zsyh:afterProductType:add")
    @Log(title = "售后产品分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZsyhAfterProductType zsyhAfterProductType)
    {
        return toAjax(zsyhAfterProductTypeService.insertZsyhAfterProductType(zsyhAfterProductType));
    }

    /**
     * 修改售后产品分类
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhAfterProductType zsyhAfterProductType = zsyhAfterProductTypeService.selectZsyhAfterProductTypeById(id);
        mmap.put("zsyhAfterProductType", zsyhAfterProductType);
        return prefix + "/edit";
    }

    /**
     * 修改保存售后产品分类
     */
    @RequiresPermissions("zsyh:afterProductType:edit")
    @Log(title = "售后产品分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZsyhAfterProductType zsyhAfterProductType)
    {
        return toAjax(zsyhAfterProductTypeService.updateZsyhAfterProductType(zsyhAfterProductType));
    }

    /**
     * 删除售后产品分类
     */
    @RequiresPermissions("zsyh:afterProductType:remove")
    @Log(title = "售后产品分类", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        //判断如果该分类下面有产品则不能删除
        ZsyhAfterProduct zsyhAfterProduct = new ZsyhAfterProduct();
        zsyhAfterProduct.setProductType(ids);
        if(zsyhAfterProductService.selectZsyhAfterProductList(zsyhAfterProduct).size()>0){
            return error("该分类下面有产品，不能删除");
        }
        return toAjax(zsyhAfterProductTypeService.deleteZsyhAfterProductTypeByIds(ids));
    }
}
